<html>
<head><meta charset="utf-8"><title>How to disable a derive macro · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html">How to disable a derive macro</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209585192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585192">(Sep 10 2020 at 00:31)</a>:</h4>
<p>If a crate <code>foo_derive</code> declares a derive macro <code>derive(Foo)</code>, is there a way to disable it using a cargo feature flag? If I just say the crate is not there, all the uses of <code>derive(Foo)</code> in my code will break, but I would like them to be ignored (produce no code) without having to annotate all uses of <code>derive(Foo)</code> with the feature flag.</p>



<a name="209585291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585291">(Sep 10 2020 at 00:33)</a>:</h4>
<p>you could make your own proc-macro crate that defines a dummy derive</p>



<a name="209585309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585309">(Sep 10 2020 at 00:33)</a>:</h4>
<p>and <code>use my_crate::Dummy as Foo;</code></p>



<a name="209585392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585392">(Sep 10 2020 at 00:34)</a>:</h4>
<p>Would you include the stub crate only if the feature is disabled?</p>



<a name="209585486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585486">(Sep 10 2020 at 00:36)</a>:</h4>
<p>sure</p>



<a name="209585506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585506">(Sep 10 2020 at 00:37)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="cp">#[cfg(feature = </span><span class="s">&quot;x&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">foo_derive</span>::<span class="n">Foo</span><span class="p">;</span><span class="w"></span>
<span class="cp">#[cfg(not(feature = </span><span class="s">&quot;x&quot;</span><span class="cp">))]</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">my_crate</span>::<span class="n">Dummy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">Foo</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<a name="209585709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585709">(Sep 10 2020 at 00:40)</a>:</h4>
<p>I mean at the toml level. Would you have both crates compiled in the Foo-for-real case?</p>



<a name="209585803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585803">(Sep 10 2020 at 00:42)</a>:</h4>
<p>oh I see, features are only additive</p>



<a name="209585806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209585806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209585806">(Sep 10 2020 at 00:42)</a>:</h4>
<p>well, not sure then</p>



<a name="209588710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209588710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209588710">(Sep 10 2020 at 01:52)</a>:</h4>
<p>Since derive is an attribute, you probably want <code>cfg_attr</code>:</p>
<p><code>#[cfg_attr(reason, derive(Thing))]</code></p>



<a name="209589030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209589030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209589030">(Sep 10 2020 at 01:58)</a>:</h4>
<p>Are you in control of all of this code?</p>



<a name="209589050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209589050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209589050">(Sep 10 2020 at 01:58)</a>:</h4>
<p>If so, you can pass a feature flag to the derive macro code and branch on it</p>



<a name="209589063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209589063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209589063">(Sep 10 2020 at 01:58)</a>:</h4>
<p><a href="https://github.com/shepmaster/snafu/blob/7fba216782cd1a0466b077b2667c15bb5f397203/snafu-derive/src/lib.rs#L1722-L1730">For example</a></p>



<a name="209593473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209593473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209593473">(Sep 10 2020 at 03:47)</a>:</h4>
<p>I did write the derive macro, so I went with that approach. It just made me wonder why all derive macros don't have a disable flag</p>



<a name="209593869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209593869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209593869">(Sep 10 2020 at 03:58)</a>:</h4>
<p>They do, cfg_attr is how you do it</p>



<a name="209629090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209629090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209629090">(Sep 10 2020 at 12:13)</a>:</h4>
<p>Because it’s not <em>usual</em> to want to disable the derive. 😅</p>



<a name="209629213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209629213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209629213">(Sep 10 2020 at 12:14)</a>:</h4>
<p>What other code do you use regularly that has a global “do nothing” switch?</p>



<a name="209629242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209629242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209629242">(Sep 10 2020 at 12:14)</a>:</h4>
<p>And, as stated, cfg_attr. <a href="https://stackoverflow.com/q/42551113/155423">https://stackoverflow.com/q/42551113/155423</a></p>



<a name="209636181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209636181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209636181">(Sep 10 2020 at 13:11)</a>:</h4>
<p>Certainly a global "do nothing" switch is a weird and undesirable thing to force on a design, but it seems like a natural consequence of the rules around derive macros living in different crates and feature flag monotonicity</p>



<a name="209643651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20disable%20a%20derive%20macro/near/209643651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20disable.20a.20derive.20macro.html#209643651">(Sep 10 2020 at 13:51)</a>:</h4>
<p>if you call a function in a crate you didn't list as a dependency you get an error, not just "do nothing". A derive is the same.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>